﻿<%@ Page Language="c#" ContentType="text/html" ResponseEncoding="utf-8" Async="true" MasterPageFile="~/administration/list_private.master" Inherits="ACM.cmsarea08.Administration.Core.gesHomePage" %>
<%@ MasterType VirtualPath="~/administration/list_private.master" %>
<asp:Content ID="PageBody" Runat="server" ContentPlaceHolderID="MasterBody">
<style type="text/css">
	.dragitem { font-size:11px; width:60px; height:60px; text-align:center; padding:5px; border:1px solid #666666; margin:5px 5px; background:#e2e3e0; float:left; }
	.artitem { display:block; width:auto; height:auto; }
	.cestino { font-size:11px; width:80px; height:80px; text-align:center; float:right; border:1px solid #666666; margin:5px 5px; background:#e2e3e0; }
	#draggables { text-align:center; }
	#homepagecc { display:block; }
	.sortme {	padding: 4px 8px;
				color: #fff;
				cursor: pointer;
				list-style: none;
				width: 300px;
				background-color: #222;
				margin:1px;
			}
	.itemlist {
		margin:2px; float:right; width:37%; border:1px solid #666666;  min-height:150px; padding:5px;
	}
	.itemlist h3 { margin:2px; padding:0px; }
</style>
<div class="dynform dfbody dfmainbody">
<h1><img src="../template/admimg/user_model_big.jpg" alt="User Forms Manager" />Gestione HomePage</h1>
<!-- modello per la gestione -->
<!-- zona dinamica -->
<div style="clear:both;">
<input type="button" value="salva" id="salva_btm" />
<input type="button" value="azzera" id="azzera_btm" />
<div style=" margin:2px; float:left; width:58%; border:2px solid #666666; background:#e2e3e0; min-height:150px; padding:5px;">
	<div id="cestino" class="cestino">cestino</div>elementi homepage:
    <ul id="homepagecc">
        <li class="sortme">0</li>
        <li class="sortme">1</li>
        <li class="sortme">2</li>
        <li class="sortme">3</li>
        <li class="sortme">4</li>
        <li class="sortme">5</li>    	
    </ul>
    
</div>
<div class="itemlist" >
	<h3>Elementi Disponibili</h3>
    <div style="clear:both;">
        <select id="elementi" name="elementi">
        <option value="-1">-- scegli --</option>
        <option value="0">Moduli</option>
        <option value="1">Blocchi Visivi</option>    
        <option value="2">Contenuti</option>        
        </select>
		<div id="choices">&nbsp;
        </div>
    </div>
	<div style="clear:both;" id="draggables">
    	<div class="dragitem">body control</div>
        <div class="dragitem">component control</div>
    
    </div>
    
</div>
<input type="text" value="" id="txtsalva"  />
<br class="clearbr"/>
</div>
<br class="clearbr"/>
</div>
<script language="javascript">
//init valori:
window.addEvent('domready', function() {
	var src_home 	= "homepagecc";
	var src_item 	= "draggables";
	var src_select 	= "elementi";
	var btmsalva 	= "salva_btm";
	var btmazzera 	= "azzera_btm";	
	var salvatxt 	= "txtsalva";	
	var currentLayout = "";
	//var temporanea
	var cur_scelta = "-1";
	
	//avvia sortable 1:
	var fxsort = $(src_home).effect('background-color', {wait: false});
	var fxcestino = $('cestino').effect('background-color', {wait: false});	
	var sortPage = null;
	
	$(btmsalva).addEvent('click', function () {
		if( $(salvatxt) && sortPage!=null) {
			alert('sto per salvare');
			var resulta = "";
			var elementi = sortPage.list.getChildren();
			elementi.each(function(element, i){
				//alert(element.innerHTML);
				var relo = element.getAttribute('rel');
				resulta += (relo?relo:element.innerHTML) + '~' + element.innerHTML+ ',';
			});
			resulta = '[' + resulta.substring(0,resulta.length-1) + ']';
			$(salvatxt).setAttribute('value',resulta);
			//fai la request in post.
			var a = new XHR({ method:'get', 
				onSuccess: function(req,xml) {
					if(xml==null) return;
					var child = xml.firstChild;
					if(parseInt(child.firstChild.nodeValue)>0) {
						alert('ok salvato');
						CaricaLayout();
					} else
						alert('impossibile salvare.');
				}
			}).send('/administration/core/xml_server.asmx/setHomePageLayout','newlayout=' + resulta);				
			
		}
	});
	$(btmazzera).addEvent('click', function () {
		CaricaLayout();
	});
	
	function InitSortabile() {
			sortPage = new Sortables($(src_home), {
				cloneOpacity:.2,
				handle: 'li',
				ghost: false,
				initialize: function(){
					this.elements.each(function(element, i){	
						
						element.addEvent('mousedown', function(e) {				
							e = new Event(e).stop();
							var clone = this.clone()
							.setStyles(this.getCoordinates()) 
							.setStyles({'opacity': 0.7, 'position': 'absolute'})
							.addEvent('emptydrop', function() {
								$('cestino').removeEvents();
								this.removeEvent();
								this.remove();
								element.removeEvents();
							}).inject(document.body);
							
							
							$('cestino').addEvent('drop', function() {
								fxcestino.start('000099').chain(fxcestino.start.pass('e2e3e0', fxcestino));	;
								element.removeEvents();						
								clone.removeEvents();						
								clone.remove();
								element.remove();
								$('cestino').removeEvents();
							});	
							$('cestino').addEvent('over', function() {					
								fxcestino.start('000099');
							});
							$('cestino').addEvent('leave', function() {					
								fxcestino.start('e2e3e0');
							});							
							
							var drag = clone.makeDraggable({
								droppables: $('cestino')
							}); 
							drag.start(e); 							
					
						});
					});
				}
			});
	
	}

	function Azzera() {
		//choices
		//draggables
		$('choices').empty();
		$('draggables').empty();
	};
	
	function ImpostaDrops() {
		$$('#draggables .dragitem').each(function(elemento){
				elemento.addEvent('mousedown', function(e) {
							e = new Event(e).stop();
							var clone = this.clone()
								.setStyles(this.getCoordinates()) // this returns an object with left/top/bottom/right, so its perfect
								.setStyles({'opacity': 0.7, 'position': 'absolute'})
								.addEvent('emptydrop', function() {
									this.remove();
									$$('#'+src_home).removeEvents();
								}).inject(document.body);
							$(src_home).addEvent('drop', function() {
								fxsort.start('7389AE').chain(fxsort.start.pass('e2e3e0', fxsort));	
								var testo = clone.innerHTML;
								var relo = clone.getAttribute('rel');
								var sortmio = new Element('li').addClass('sortme').setHTML(testo);
								if(relo)
									sortmio.setAttribute('rel',relo);
								clone.remove();
								//verifica figli in disortmio e casomai elimina.
								var checkFigli = $$('#' +src_home + ' li');
								if( checkFigli.length > 0 ) {
									if(checkFigli.length == 1) {
										if(checkFigli[0].getAttribute('rel')=='empty' ) {
											checkFigli[0].removeEvents();
											checkFigli[0].remove();											
										}
									}
								}
								this.adopt(sortmio);
								//riavvia sort.
								InitSortabile();
							});
							$(src_home).addEvent('over', function() {
								fxsort.start('7389AE');	
							});
							$(src_home).addEvent('leave', function() {
								fxsort.start('e2e3e0');	
							});							

							var drag = clone.makeDraggable({
								droppables: $(src_home)
							}); // this returns the dragged element
				
							drag.start(e); // start the event manual
							
						});
				});
	}
	
	function initCategorie(container) {
		if(container.value==-1) {
			if( $('mio_cat') ) Element.remove($('mio_cat'));
			$('draggables').empty();
			return;
		}
		if( $('mio_cat')     ) {
			Element.remove($('mio_cat'));
			$('draggables').empty();
		}
		var mio_cat = new Element('select').setProperty('id', 'mio_cat').injectInside( $('choices') );
		new Element('option').setProperty('value','-1').setHTML('-- categorie --').injectInside( mio_cat );
			var a = new XHR({ method:'get', 
				onSuccess: function(req,xml) {
					if(xml==null) return;
					var child = xml.firstChild;
					if(child.nodeName !="NewDataSet") return;
					var xml_table = child.firstChild;
					while (xml_table) {
						if(xml_table.nodeName=='Table') {
							//1 id , 3 titolo
							var test_ti = xml_table.childNodes[3].firstChild.nodeValue;
							var test_di = xml_table.childNodes[1].firstChild.nodeValue;
							new Element('option').setProperty('value',test_di).setHTML(test_ti).injectInside(mio_cat );
						}
						xml_table = xml_table.nextSibling;
					}
				}
			}).send('/administration/core/xml_server.asmx/getCategory','idsec=' + container.value);	
		//appendi evento:
		mio_cat.addEvent('change', function() {
			if(this.value=="-1") return;
			$('draggables').empty();
			//attiva drop:
			var ahxr = new XHR({ method:'get', 
				onSuccess: function(req,xml) {
					if(xml==null) return;
					var child = xml.firstChild;
					if(child.nodeName !="NewDataSet") return;
					var xml_table = child.firstChild;
					while (xml_table) {
						if(xml_table.nodeName=='Table') {
							//1 id , 3 titolo
							var test_ti = xml_table.childNodes[3].firstChild.nodeValue;
							var test_di = xml_table.childNodes[1].firstChild.nodeValue;
							var pin = new Element('div').setProperty('class','dragitem artitem').setHTML(test_ti).injectInside( $('draggables') );
							pin.setAttribute('rel',"CT~" + test_di);
						}
						xml_table = xml_table.nextSibling;
					}
					ImpostaDrops();
				}
			}).send('/administration/core/xml_server.asmx/getArticoli','idcat=' + this.value);				
//			new Element('div').setProperty('class','dragitem').setHTML('art v1_' + this.value).injectInside( $('draggables') );
//			new Element('div').setProperty('class','dragitem').setHTML('art v2_' + this.value).injectInside( $('draggables') );
			ImpostaDrops();
		});
	}

	function Carica(scelta) {
		//scelta 0 --> moduli
		if(scelta=='0') {
			var sel_mod = new Element('select').setProperty('id', 'choose_mod').injectInside( $('choices') );
			sel_mod.addEvent('change',function() { 
							//load elementi:
							$('draggables').empty();
							if(this.value==-1) return;
							//ottieni configurazione ed elemento body, o compo
							///administration/core/xml_server.asmx/getModulesElement?idmod=string
							var valore = this.value;
							var testo  = "";
							for(var i = 0;i<this.options.length;i++) {
								if(this.options[i].selected) {
									testo = this.options[i].innerHTML;
									break;
								}
							}
							var aint = new XHR({ method:'get', 
								onSuccess: function(req,xml) {
									if(xml==null) return;
									var child = xml.firstChild;
									if(child.nodeName !="NewDataSet") return;
									var xml_table = child.firstChild;
									while (xml_table) {
										if(xml_table.nodeName=='Table') {
											//1 id , 3 titolo
											var mbody="";
											var mcontrol ="";
											for(var i = 0 ; i<xml_table.childNodes.length;i++) {
												if (xml_table.childNodes[i].nodeType != 1) continue;
												if(xml_table.childNodes[i].nodeName=='body') {
													if(xml_table.childNodes[i].hasChildNodes) {
														if(xml_table.childNodes[i].firstChild)
															mbody = xml_table.childNodes[i].firstChild.nodeValue;
													}
												}
												if(xml_table.childNodes[i].nodeName=='control') {
													if(xml_table.childNodes[i].hasChildNodes) {
														if(xml_table.childNodes[i].firstChild)
															mcontrol = xml_table.childNodes[i].firstChild.nodeValue;
													}
												}
											}
											if(mbody!="") {
												var bd = new Element('div').setProperty('class','dragitem').setHTML(testo+ ' body control').injectInside( $('draggables') );
												bd.setAttribute('rel','MDB~' + valore);
											}
											if(mcontrol!="") {
												var bc = new Element('div').setProperty('class','dragitem').setHTML(testo+ ' control').injectInside( $('draggables') );
												bc.setAttribute('rel','MDC~' + valore);
											}											
											//new Element('option').setProperty('value',test_di).setHTML(test_ti).injectInside(sel_mod );
										}
										xml_table = xml_table.nextSibling;
									}
									ImpostaDrops();
								}
								
							}).send('/administration/core/xml_server.asmx/getModulesElement','idmod='+this.value);								
//							new Element('div').setProperty('class','dragitem').setHTML('body control ' + this.value).injectInside( $('draggables') );
//							new Element('div').setProperty('class','dragitem').setHTML('component control ' + this.value).injectInside( $('draggables') );
							//init drag.
//							ImpostaDrops();
						 });
			//ottieni elementi ajax per sel_mod...
			new Element('option').setProperty('value','-1').setHTML('-- scegli modulo --').injectInside( sel_mod );
			var a = new XHR({ method:'get', 
				onSuccess: function(req,xml) {
					if(xml==null) return;
					var child = xml.firstChild;
					if(child.nodeName !="NewDataSet") return;
					var xml_table = child.firstChild;
					while (xml_table) {
						if(xml_table.nodeName=='Table') {
							//1 id , 3 titolo
							var test_ti = xml_table.childNodes[3].firstChild.nodeValue;
							var test_di = xml_table.childNodes[1].firstChild.nodeValue;
							/*
							for(var i = 0 ; i<xml_table.childNodes.length;i++) {
								if (xml_table.childNodes[i].nodeType != 1) continue;
								alert(xml_table.childNodes[i].nodeName + " --" + xml_table.childNodes[i].nodeValue + " --("+i+")");
							}
							*/	
							new Element('option').setProperty('value',test_di).setHTML(test_ti).injectInside(sel_mod );
						}
						xml_table = xml_table.nextSibling;
					}
				}
			}).send('/administration/core/xml_server.asmx/getModules');			
			return;
		}
		//scelta 1 --> bv;
		if(scelta=='1') {
			$('draggables').empty();
			//--/administration/core/xml_server.asmx/getBlockViews
			var a = new XHR({ method:'get', 
				onSuccess: function(req,xml) {
					if(xml==null) return;
					var child = xml.firstChild;
					if(child.nodeName !="NewDataSet") return;
					var xml_table = child.firstChild;
					while (xml_table) {
						if(xml_table.nodeName=='Table') {
							//1 id , 3 titolo
							var test_ti = xml_table.childNodes[3].firstChild.nodeValue;
							var test_di = xml_table.childNodes[1].firstChild.nodeValue;
							/*
							for(var i = 0 ; i<xml_table.childNodes.length;i++) {
								if (xml_table.childNodes[i].nodeType != 1) continue;
								alert(xml_table.childNodes[i].nodeName + " --" + xml_table.childNodes[i].nodeValue + " --("+i+")");
							}
							*/	
							var uno  = new Element('div').setProperty('class','dragitem').setHTML(test_ti).injectInside( $('draggables') );
							uno.setAttribute('rel', 'BV~' +test_di);
						}
						xml_table = xml_table.nextSibling;
					}
					ImpostaDrops();
				}
			}).send('/administration/core/xml_server.asmx/getBlockViews');
			
			return;
		}
		//scelta 2 --> contenuti
		if(scelta=='2') {
			var sec_mod = new Element('select').setProperty('id', 'choose_sec').injectInside( $('choices') );
			//aggiungo le opzioni
			new Element('option').setProperty('value','-1').setHTML('-- scegli sezione').injectInside( sec_mod );
			var a = new XHR({ method:'get', 
				onSuccess: function(req,xml) {
					if(xml==null) return;
					var child = xml.firstChild;
					if(child.nodeName !="NewDataSet") return;
					var xml_table = child.firstChild;
					while (xml_table) {
						if(xml_table.nodeName=='Table') {
							//1 id , 3 titolo
							var test_ti = xml_table.childNodes[3].firstChild.nodeValue;
							var test_di = xml_table.childNodes[1].firstChild.nodeValue;
							/*
							for(var i = 0 ; i<xml_table.childNodes.length;i++) {
								if (xml_table.childNodes[i].nodeType != 1) continue;
								alert(xml_table.childNodes[i].nodeName + " --" + xml_table.childNodes[i].nodeValue + " --("+i+")");
							}
							*/	
							new Element('option').setProperty('value',test_di).setHTML(test_ti).injectInside(sec_mod );
						}
						xml_table = xml_table.nextSibling;
					}
				}
			}).send('/administration/core/xml_server.asmx/getSection');				
			sec_mod.addEvent('change',function() {
				//sto dentro
				if(this.value==-1) return;
				initCategorie(this);
			});
			//ottieni elementi ajax per sel_mod...
			return;
		}
	
	
	}
	
	
	var cambioScelta = function() {
		if(cur_scelta != this.value)  {
			//alert('nuova scelta' + this.value);
			cur_scelta = this.value;
			Azzera();
			Carica(cur_scelta);
		}
	};	
	
	function NoLayout() {
		$(src_home).empty();
		var empty = new Element('li').setHTML("empty").injectInside( $(src_home) );
		empty.setAttribute('rel','empty');
	}
	
	function CaricaLayout() {
		//richiedi layout. - ottieni l'elenco e compila la stringa di input.
		//svuota  --> getHomePageLayout
		NoLayout();
		
		//load
		var a = new XHR({ method:'get', 
				onSuccess: function(req,xml) {
						if(xml==null) return;
						var child = xml.firstChild;
						if(child.nodeName !="NewDataSet") return;
						var xml_table = child.firstChild;
						$(src_home).empty();
						while (xml_table) {
							if(xml_table.nodeName=='Table') {
								//1 id , 3 itemtype, 5 itemid, 7 ordine, 9 titolo
								var itype = "";
								var iid   = "";
								var ititle = "";
								if(xml_table.childNodes[3]) {
									if(xml_table.childNodes[3].firstChild)
										itype = xml_table.childNodes[3].firstChild.nodeValue;
								}
								if(xml_table.childNodes[5]) {
									if(xml_table.childNodes[5].firstChild)
										iid = xml_table.childNodes[5].firstChild.nodeValue;
								}	
								if(xml_table.childNodes[9]) {
									if(xml_table.childNodes[9].firstChild)
										ititle = xml_table.childNodes[9].firstChild.nodeValue;
								}	
								
								if(itype!="" && iid!="") {
									var lio =  new Element('li').setProperty('class','sortme').setHTML(ititle).injectInside($(src_home) );
									lio.setAttribute('rel', itype + '~' + iid);
								}							
	//							new Element('option').setProperty('value',test_di).setHTML(test_ti).injectInside(sec_mod );
							}
							xml_table = xml_table.nextSibling;
						}
						if( $$('#'+src_home + ' li').length==0 ) {
							NoLayout();
						} else InitSortabile();
				} ,
				onFailure : function() {
					NoLayout();
				}
			}).send('/administration/core/xml_server.asmx/getHomePageLayout');				
	
	}	
	
	//avvia handler per i loader:
	var selettore = $(src_select);
	selettore.addEvent('change',cambioScelta);
	
	


	
	//avvia sort:
	CaricaLayout();
	InitSortabile();
	Azzera();
	

});
</script>

</asp:content>